//
//  LongPickerTagView.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/20.
//

#import "LongPickerTagView.h"

@interface LongPickerTagView()<LongTagViewDelegate>
@property (nonatomic, strong) LongTagView *tagView;
@property (strong, nonatomic) UIView *bjview;
@property (nonatomic,strong) UIButton *closeBut;
@property (nonatomic,strong) UIButton *determineBut;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger chooseTag;
@end

@implementation LongPickerTagView
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArr = dataArr;
        [self WKLSubViews];
    }
    return self;
}

// 重写set属性
- (void)setMaxnumber:(NSInteger)maxnumber{
    _maxnumber = maxnumber;
    self.tagView.maxnumber = maxnumber;
    if (maxnumber == 1) {
        self.tagView.multiselect = NO;
    }
}

- (void)setWidthNumber:(NSInteger)widthNumber{
    _widthNumber = widthNumber;
    self.tagView.widthNumber = widthNumber;
    self.tagView.automaticWidth = widthNumber == 0?YES:NO;
}


- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.dataSource removeAllObjects];
    self.tagView.tagArray = dataArr;
    self.tagView.maxnumber = self.maxnumber;
}

-(void)WKLSubViews{
    
    self.backgroundColor = rgba(150, 150, 150, 0.6);
    self.bjview = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-290, APPwidth, 290)];
    self.bjview.backgroundColor = [UIColor whiteColor];
//    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bjview.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(12, 12)];
//    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//    maskLayer.frame = self.bjview.bounds;
//    maskLayer.path = maskPath.CGPath;
//    self.bjview.layer.mask = maskLayer;
    [self addSubview:self.bjview];
    
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, APPwidth-120, 30)];
    self.titleLabel.text = @"请选择标签";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bjview addSubview:self.titleLabel];
    
    
    self.closeBut = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, 40, 35)];
    [self.closeBut setTitle:@"取消" forState:UIControlStateNormal];
    [self.closeBut setTitleColor:rgba(153, 153, 153, 1) forState:UIControlStateNormal];
    self.closeBut.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.closeBut addTarget:self action:@selector(closeButCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.bjview addSubview:self.closeBut];
    
    
    self.determineBut = [[UIButton alloc]initWithFrame:CGRectMake(APPwidth-55, 10, 40, 35)];
    [self.determineBut setTitle:@"确定" forState:UIControlStateNormal];
    [self.determineBut setTitleColor:rgba(22, 120, 255, 1) forState:UIControlStateNormal];
    self.determineBut.titleLabel.font = [UIFont systemFontOfSize:17];;
    [self.determineBut addTarget:self action:@selector(determineButCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.bjview addSubview:self.determineBut];
    
    
    [self MyPickerView];
}
#pragma mark - 设置选择器
-(void)MyPickerView
{
    self.tagView = [[LongTagView alloc]initWithFrame: CGRectMake(15, 45, APPwidth-30, 245)];
    self.tagView.textColorNormal = rgba(51, 51, 51, 1);
    self.tagView.backgroundColorNormal = rgba(245, 245, 250, 1);
    self.tagView.textColorSelected = rgba(255, 255, 255, 1);
    self.tagView.backgroundColorSelected = rgba(22, 120, 255, 1);
    self.tagView.textFontNormal = [UIFont systemFontOfSize:14];
    self.tagView.btnH = 28;
    self.tagView.multiselect = YES;
    self.tagView.Delegate = self;
    [self.bjview addSubview:self.tagView];
}



- (void)DeleteWithTagViewTitleArr:(NSArray *)titilearr
{
    NSLog(@"titilearr == %@",titilearr);
    [self.dataSource removeAllObjects];
    for (NSString *str in titilearr) {
        [self.dataSource addObject:str];
    }
    
}

-(void)DeleteWithTagViewTitle:(NSString *)titile WhitTag:(NSInteger)tag
{
    self.chooseTag = tag;
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:titile];
}

- (void)getViewHeight:(CGFloat)height
{
    NSLog(@"height == %f",height);
    
    self.tagView.frame = CGRectMake(15, 45, APPwidth-30, height);
    self.bjview.frame = CGRectMake(0, self.frame.size.height-height-45-50, APPwidth, height+95);
    
}



-(void)determineButCheng:(UIButton *)but
{
    if (self.maxnumber == 1&&!self.dataSource.count) {
        [self.dataSource addObject:self.dataArr.firstObject];
    }
    
    if ([self.delegate respondsToSelector:@selector(choosPickerView:WithchoosStrArr:)]) {
        [self.delegate choosPickerView:self WithchoosStrArr:self.dataSource];
    }
    self.hidden = YES;
}
-(void)closeButCheng:(UIButton *)but
{
    if ([self.delegate respondsToSelector:@selector(closePickerTagView:)]) {
        [self.delegate closePickerTagView:self];
    }
    
    self.hidden = YES;
}

@end
